<template>
	<view>
		<form @submit="formSubmit">
			<view class="cu-form-group">
				<view class="title">标&nbsp;&nbsp;题</view>
				<input name="title" placeholder="请填写" type="text" v-model="pc.title"/>
			</view>
			<view class="cu-form-group">
				<view class="title">生长期</view>
				<input name="growthPhase" placeholder="请选择"  v-model="pc.growthPhase" disabled=""></input>
				<button class="cu-btn bg-green shadow" @tap="showModal" data-target="pModal">选择</button>
			</view>

			<view class="cu-form-group">
				<view class="title">日期</view>
				<picker name="date"  mode="date"  v-model="pc.date"  :value="date" :start="startDate" :end="endDate" @change="bindDateChange" >
					<view class="picker">
						{{date}}
					</view>
				</picker>
			</view>
			<view class="cu-form-group">
				<textarea   placeholder="活动内容" name="description" v-model="pc.description"></textarea>
			</view>
			<view class="cu-bar btn-group margin-tb-sm">
				<button class="cu-btn bg-green shadow-blur round lg"  form-type="submit">提交</button>
			</view>
		</form>

		<view class="cu-modal" :class="modalName=='pModal'?'show':''" @tap="hideModal">
			<view class="cu-dialog" @tap.stop="">
				<radio-group class="block" @change="szqChange">
					<view class="cu-list menu text-left">
						<view class="cu-item" v-for="(item,index) in periodList" :key="index" @tap="szqClick" :data-id="item.id" :data-name="item.name">
							<label class="flex justify-between align-center flex-sub">
								<view class="flex-sub">{{item.name}}</view>
								<radio class="type" :class="pc.growthPhaseId==item.id?'checked':''" :checked="pc.growthPhaseId==item.id?true:false" :value="item.name"></radio>
							</label>
						</view>
					</view>
				</radio-group>
			</view>
		</view>
	</view>
</template>

<script>
	//获取日期
	function getDate(type) {
		const date = new Date();
	
		let year = date.getFullYear();
		let month = date.getMonth() + 1;
		let day = date.getDate();
	
		if (type === 'start') {
			year = year - 5;
		} else if (type === 'end') {
			year = year;
		}
		month = month > 9 ? month : '0' + month;;
		day = day > 9 ? day : '0' + day;
	
		return `${year}-${month}-${day}`;
	}
	export default {
		data() {
			return {
				date: getDate({
					format: true
				}),
				//时间选择限制条件-start
				startDate: getDate('start'),
				//时间选择限制条件-end
				endDate: getDate('end'),
				
				modalName:'',
				periodList:[],
				pc:{
					id:'',
					plantledgerDetailId:'',//种植明细主键
					title:'',
					growthPhaseId:'',//生长期id
					growthPhase:'',
					date:'',
					description:'',
					landId:'',
					startDate:'',
					endDate:''
				},
				plantledgerDetail:''
			};
		},
		onLoad(options) {
			let param=this.$Route.query;
			let flag=param.flag;
			if('edit'==flag){
				this.pc=param;
				this.date=this.pc.date==null?this.date:this.pc.date;
			}else{
				this.pc.landId=param.landId;
				this.pc.startDate=param.startDate;
				this.pc.endDate=param.endDate;
				this.pc.plantledgerDetailId=param.id;
			}
			this.getPd();
		},
		methods: {
			showModal(e) {
				this.getPeriodList()
				this.modalName = e.currentTarget.dataset.target
			},
			hideModal(e) {
				this.modalName = null
			},
			verify(e) {
				e.detail.value = e.detail.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符  
				e.detail.value = e.detail.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的  
				e.detail.value = e.detail.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
				e.detail.value = e.detail.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, '$1$2.$3'); //只能输入两个小数  
				if (e.detail.value.indexOf(".") < 0 && e.detail.value != "") { //以上已经过滤，此处控制的是如果没有小数点，首位不能为类似于 01、02的金额 
					e.detail.value = parseFloat(e.detail.value);
				} else if (e.detail.value.indexOf(".") == 0) { //首为小数点，补全
					e.detail.value = e.detail.value.replace(/[^$#$]/g, "0.");
					e.detail.value = e.detail.value.replace(/\.{2,}/g, ".");
				}
			},
			szqClick(e) {
				this.pc.growthPhaseId = e.currentTarget.dataset.id;
				this.pc.growthPhase = e.currentTarget.dataset.name;
			},
			szqChange(e) {
				
			},
			//获取种植明细
			getPd() {
				uni.request({
					url: this.$config.apiUrl + '/dasPlantledgerController/getPd/'+this.pc.plantledgerDetailId,
					success: (res) => {
						if (res.data.ok) {
							this.plantledgerDetail = res.data.data
						} else {
							uni.showToast({
								icon: "none",
								title: '获取作物生长期失败',
								duration: 1000
							});
						}
					},
					fail() {
						uni.showModal({
							title: '提示',
							content: "获取作物生长期失败",
							showCancel: false
						});
					}
				});
			},
			//种植明细
			getPdList() {
				uni.request({
					url: this.$config.apiUrl + '/cropController/getPeriodList',
					data: {
						cropId: this.plantledgerDetail.cropId
					},
					success: (res) => {
						if (res.data.ok) {
							this.periodList = res.data.data
						} else {
							uni.showToast({
								icon: "none",
								title: '获取作物生长期失败',
								duration: 1000
							});
						}
					},
					fail() {
						uni.showModal({
							title: '提示',
							content: "获取作物生长期失败",
							showCancel: false
						});
					}
				});
			},
			//生长期
			getPeriodList() {
				uni.request({
					url: this.$config.apiUrl + '/cropController/getPeriodList',
					data: {
						cropId: this.plantledgerDetail.cropId
					},
					success: (res) => {
						if (res.data.ok) {
							this.periodList = res.data.data
						} else {
							uni.showToast({
								icon: "none",
								title: '获取作物生长期失败',
								duration: 1000
							});
						}
					},
					fail() {
						uni.showModal({
							title: '提示',
							content: "获取作物生长期失败",
							showCancel: false
						});
					}
				});
			},
			bindDateChange: function(e) {
				this.date = e.detail.value
			},
			formSubmit: function(e) {
				let formdata=e.detail.value;
				formdata.id = this.pc.id;
				formdata.plantledgerDetailId = this.pc.plantledgerDetailId;
				formdata.growthPhaseId=this.pc.growthPhaseId;
				formdata.growthPhase=this.pc.growthPhase;
				formdata.date=this.date;
				if (formdata.title == '') {
					this.showData('请输入标题');
				} else if (formdata.growthPhaseId == '') {
					this.showData('请选择生长期')
				} else if (formdata.date == '') {
					this.showData("请选择日期");
				} else if (formdata.description == '') {
					this.showData("请输入活动信息");
				} else {
					uni.showLoading({
						title: '加载中'
					});
					uni.request({
						url: this.$config.apiUrl + '/dasPlantledgerController/savePlantactivity',
						method: 'POST',
						header: {
							'content-type': 'application/x-www-form-urlencoded'//这样设置request才能接值
						},
						data:formdata,
						success: (res) => {
							if (res.data.ok) {
								uni.hideLoading();
								uni.showToast({
									title: '保存成功',
									duration: 3000
								});
								uni.redirectTo({
									url: './zzda_detail?landId=' + this.pc.landId + '&startDate=' + this.pc.startDate + '&endDate=' + this.pc.endDate
								})
							} else {
								uni.hideLoading();
								uni.showToast({
									icon: "none",
									title: '保存失败',
									duration: 1000
								});
							}
						},
						fail() {
							uni.hideLoading();
							uni.showModal({
								title: '提示',
								content: "保存失败",
								showCancel: false
							});
						}
					});
				}
			},
			showData(str) {
				uni.showToast({
					icon: "none",
					title: str,
					duration: 2000
				})
			}
		}
	}
</script>

<style>
</style>
